package com.etbsms.form;

import java.util.List;

/**
 * 分頁資訊表單
 * @author Administrator
 *
 */

@SuppressWarnings("unused")
public class PageForm<T>{	
	
	private int rowsPerPage;							/* 每頁筆數					*/
	private int totalRows;								/* 總筆數					*/
	private int totalPages;							/* 總頁數					*/	
	private int nowPage;									/* 目前所在頁數				*/
	private int firstResult;							/* 目前所在頁數的第一筆數	*/	
	private int startPage;								/* 顯示第一頁數				*/
	private int endPage;									/* 顯示最後頁數				*/
	private boolean requery;						/* 是否重新查詢,[true]則重新撈總筆數	*/	
	private String clickFuncionName;			/* javascript 上分頁的function 名稱 	*/
	private String pageCssStyleName;
	private String thisPageSeqs;					/* 批次修改時此頁的全部seq 	*/
	private String pageInfo;	
	private List<T> obj;
	
	/** 對應[我的表單]**/
	private String queryType = "9,";
	
	/** 對應[我的表單] 基本表單優先 **/
	private boolean basicFormPriority = true;

	public PageForm(){
		this(10);	
	}

	public PageForm(int rowsPerPage){
		this.rowsPerPage = rowsPerPage;
		this.nowPage = 1;
		this.requery = true;
		this.clickFuncionName = "pageTo";
		this.pageCssStyleName = "pagination flickr";	
	}	
	
	public int getFirstResult() {
		firstResult = (this.getNowPage() - 1) * this.getRowsPerPage();
		return firstResult;
	}
	public int getRowsPerPage() {
		return rowsPerPage;
	}
	public void setRowsPerPage(int rowsPerPage) {
		this.rowsPerPage = rowsPerPage;
	}
	public int getTotalRows() {
		return totalRows;
	}
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}
	public int getTotalPages() {
		int _pages = getTotalRows() / getRowsPerPage();
		boolean _isAdd1 = getTotalRows() % getRowsPerPage() > 0;
		totalPages = _isAdd1 ? _pages + 1 : _pages ;		
		return totalPages;
	}	
	public int getNowPage() {
		return nowPage;
	}
	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}
	public int getStartPage() {
		int sPage = getEndPage() - 9 < 1 ? 
				1 : getEndPage() - 9;	
		return sPage;
	}	
	public int getEndPage() {
		int ePage = getNowPage() + 9 > getTotalPages() ? 
				(getNowPage() + 6) > getTotalPages() ? 
						getTotalPages() : getTotalPages() <= 10 ? 
								getTotalPages() : (getNowPage() + 6) : (getNowPage() <= 4) ? 
										10 : getNowPage() + 6;
		return ePage;
	}
	public List<T> getObj() {
		return obj;
	}
	public void setObj(List<T> obj) {
		this.obj = obj;
	}	
	public String getClickFuncionName() {
		return clickFuncionName;
	}
	public void setClickFuncionName(String clickFuncionName) {
		this.clickFuncionName = clickFuncionName;
	}
	public String getPageCssStyleName() {
		return pageCssStyleName;
	}
	public void setPageCssStyleName(String pageCssStyleName) {
		this.pageCssStyleName = pageCssStyleName;
	}	
	public boolean isRequery() {
		return requery;
	}
	public void setRequery(boolean requery) {
		this.requery = requery;
	}	
	public String getPageInfo(){
		String _href = "href='javascript:void(0);'";
		String _firstPage = "<li class='disabled'>第一頁</li>";
		String _prevPage  = "<li class='disabled'>上一頁</li>";
		String _nextPage  = "<li class='disabled'>下一頁</li>";
		String _lastPage  = "<li class='disabled'>最後一頁</li>";		
		StringBuffer sb = new StringBuffer();
		sb.append("<div class='"+this.getPageCssStyleName()+"'><ul>");
		
		if(this.getNowPage() != 1){
			_firstPage = "<li><a " + _href + _clickFunc(1) + ">第一頁</a></li>";
			_prevPage = "<li><a " + _href + _clickFunc(this.getNowPage() - 1) + ">上一頁</a></li>";
		}		
		if(this.getNowPage() != this.getTotalPages()){
			_nextPage = "<li><a " + _href + _clickFunc(this.getNowPage() + 1) + ">下一頁</a></li>";
			_lastPage = "<li><a " + _href + _clickFunc(this.getTotalPages()) + ">最後一頁</a></li>";			
		}		
		
		sb.append(_firstPage + _prevPage);
		for(int i=this.getStartPage();i<=this.getEndPage();i++){
			if(this.getNowPage() == i)				
				sb.append("<li class='current'>" + i + "</li>");
			else
				sb.append("<li><a " + _href + _clickFunc(i) + ">" + i + "</a></li>");
		}
		sb.append(_nextPage + _lastPage);
		sb.append("<li><a href='javascript:void(0);' style='cursor: text;'>共"+this.getTotalRows()+"筆&nbsp;/&nbsp;"+this.getTotalPages()+"頁</a></li>");	
		sb.append("</ul></div>");
		return this.getTotalPages() <= 1 ? "" : sb.toString();		
	}
	
	@Override
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("rowsPerPage\t(每頁筆數)\t\t:" + this.getRowsPerPage()+"\n");
		sb.append("totalRows\t(總筆數)\t\t\t:" + this.getTotalRows()+"\n");
		sb.append("totalPages\t(總頁數)\t\t\t:" + this.getTotalPages()+"\n");
		sb.append("nowPage\t\t(目前所在頁數)\t\t:" + this.getNowPage()+"\n");
		sb.append("firstResult\t(目前所在頁數的第一筆數)\t:" + this.getFirstResult()+"\n");
		sb.append("startPage\t(顯示第一頁數)\t\t:" + this.getStartPage()+"\n");
		sb.append("endPage\t\t(顯示最後頁數)\t\t:" + this.getEndPage()+"\n");
		System.out.println(sb);
		return sb.toString();
	}
	
	private String _clickFunc(int number){
		return " onclick='"+this.getClickFuncionName()+"("+number+");'";		 
	}

	public void setThisPageSeqs(String thisPageSeqs) {
		this.thisPageSeqs = thisPageSeqs;
	}

	public String getThisPageSeqs() {
		if(thisPageSeqs.length() > 0)
			thisPageSeqs = thisPageSeqs.substring(0,thisPageSeqs.length() - 1);
		return thisPageSeqs;
	}
	
	public String getQueryType() {
		return queryType;
	}

	public void setQueryType(String queryType) {
		this.queryType = queryType;
	}

	public boolean isBasicFormPriority() {
		return basicFormPriority;
	}

	public void setBasicFormPriority(boolean basicFormPriority) {
		this.basicFormPriority = basicFormPriority;
	}	
}
